package com.infore.airsupervise.biz.impl;

import android.util.Log;

import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.app.StatusConst;
import com.infore.airsupervise.bean.AQIRealDataE;
import com.infore.airsupervise.bean.BaseBean;
import com.infore.airsupervise.bean.EventDetailsE;
import com.infore.airsupervise.bean.EventE;
import com.infore.airsupervise.bean.StationExpandE;
import com.infore.airsupervise.biz.EventBiz;
import com.infore.airsupervise.http.BaseResponse;
import com.infore.airsupervise.http.HttpAPI;
import com.infore.airsupervise.http.HttpCallBack;
import com.infore.airsupervise.http.HttpUtil;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.JSONUtil;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

import static com.infore.airsupervise.http.HttpAPI.ROOT_URL;

/**
 * Created by Administrator on 2017/8/18.
 */

public class EventBizImpl implements EventBiz {
    @Override
    public Call<BaseResponse> getEventList(String datatype, int pagenum,int recordtype, final OnServerResponseListener<List<EventE>> listener) {
        Map<String,Object> params= new HashMap<>();
        params.put("dataType",datatype);
        params.put("pageNum",pagenum);
        params.put("recordType",recordtype);
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.EVENT_RECORD_LIST,params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);

                List<EventE> list = JSONUtil.parseList(response.getData(), Constants.RESPONSE_BEAN_LIST_KEY,EventE.class);
                listener.onSucceed(list,response.getMessage());

            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return call;
    }

    @Override
    public Call<BaseResponse> getSearchEventList(String keyword, int datatype, int pagenum, final OnServerResponseListener<List<EventE>> listener) {
        Map<String,Object> params= new HashMap<>();
        params.put("keyword",keyword);
        params.put("dataType",datatype);
        params.put("pageNum",pagenum);

        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.SEARCH_EVENT_RECORD_LIST,params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);

                List<EventE> list = JSONUtil.parseList(response.getData(), Constants.RESPONSE_BEAN_LIST_KEY,EventE.class);
                listener.onSucceed(list,response.getMessage());

            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });


        return call;
    }

    @Override
    public Call<BaseResponse> getSearchHandleEventList(String keyWord, int pageNum, final OnServerResponseListener<List<EventE>> listener) {
        // 判断网络是否可用
        if (!HttpUtil.getInstance().isNetworkOk()) {
            listener.onFailed(StatusConst.CODE_CONNECT_FAIL, MsgConst.CONNECT_WRONG, MsgConst.PLEASE_CHECK_NETWORK);
            return null;
        }
        Map<String,Object> params = new HashMap<>();
        params.put(Constants.PAGER_NUM_KEY , pageNum);
        params.put("keyword",keyWord);
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.SEARCH_HANDLE_RECOD_LIST,params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<EventE> list = JSONUtil.parseList(response.getData(), Constants.RESPONSE_BEAN_LIST_KEY,EventE.class);
                listener.onSucceed(list,response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });
        return call;
    }

    @Override
    public Call<BaseResponse> getEventDetails(String eventid, final OnServerResponseListener<EventDetailsE> listener) {

        Map<String,Object> params= new HashMap<>();
        params.put("recordId",eventid);


        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.EVENT_RECORD_DETAILS,params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);

                EventDetailsE parseBean = JSONUtil.parseBean(response.getData(), Constants.RESPONSE_BEAN_KEY,EventDetailsE.class);
                listener.onSucceed(parseBean,response.getMessage());

            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });
        return call;
    }

    @Override
    public Call<BaseResponse> getHandlerEventVerify(String eventid, final OnServerResponseListener<BaseBean> listener) {
        Map<String,Object> params= new HashMap<>();
        params.put("recordId",eventid);


        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.EVENT_RECORD_VERIFY,params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                String  msg=response.getMessage();
                int status=response.getStatus();
                BaseBean baseBean=new BaseBean();
                baseBean.setMessage(msg);
                baseBean.setStatus(status);
                listener.onSucceed(baseBean,msg);

            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                Log.e("operationCode",operationCode+"");
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });
        return null;
    }

    @Override
    public Call<BaseResponse> submitEventData(Map<String, Object> params, List<String> filePaths, final OnServerResponseListener<BaseBean> listener) {

        // 发起请求
        Call<BaseResponse> call = HttpUtil.getHttpService().postFiles(HttpAPI.SUBMIT_EVENT_DATA,
                HttpUtil.getInstance().filesToBodyParts("file",filePaths),
                HttpUtil.getInstance().paramsToBodyParts(params));
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                // 注意：提交并处理时Message为故障id

                String  msg=response.getMessage();
                int status=response.getStatus();
                BaseBean baseBean=new BaseBean();
                baseBean.setMessage(msg);
                baseBean.setStatus(status);
                listener.onSucceed(baseBean,msg);
            }

            @Override
            public void onFailed(int operationCode, String message) {
                super.onFailed(operationCode, message);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED,message);
            }

            @Override
            public void onException(int errorCode, String msgTitle, String msgDesc) {
                super.onException(errorCode, msgTitle, msgDesc);
                listener.onFailed(errorCode, msgTitle, msgDesc);
            }
        });




        return null;
    }

    @Override
    public Call<BaseResponse> getStationTreeRequest(final OnServerResponseListener<List<StationExpandE>> listener) {

        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_STATION_TREE, Collections.EMPTY_MAP);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<StationExpandE> beanList = JSONUtil.parseList(
                        response.getData(), Constants.RESPONSE_BEAN_LIST_KEY, StationExpandE.class);
                listener.onSucceed(beanList, response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });
        return null;
    }

    @Override
    public Call<BaseResponse> submitCreateEventData(Map<String, Object> params, final OnServerResponseListener<BaseResponse> listener) {
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.SUBMIT_CREATE_EVENT_DATA, params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                if(response.getStatus()==1){
                    listener.onSucceed(response, response.getMessage());
                }

            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return null;
    }

    @Override
    public Call<BaseResponse> submitEditorEventData(Map<String, Object> params, final OnServerResponseListener<BaseResponse> listener) {
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.SUBMIT_EDITOR_EVENT_DATA, params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                if(response.getStatus()==1){
                    listener.onSucceed(response, response.getMessage());
                }

            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return null;
    }

    @Override
    public Call<BaseResponse> getHandlerEventList(int pagenum, final OnServerResponseListener<List<EventE>> listener) {
        Map<String,Object> params= new HashMap<>();

        params.put("pageNum",pagenum);

        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.HANDLE_EVENT_RECORD_LIST,params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);

                List<EventE> list = JSONUtil.parseList(response.getData(), Constants.RESPONSE_BEAN_LIST_KEY,EventE.class);
                listener.onSucceed(list,response.getMessage());

            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return null;
    }
}
